package com.hyacinth.core.sysmgr.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hyacinth.common.constant.SysConstant;
import com.hyacinth.core.sysmgr.domain.SysMenu;
import com.hyacinth.core.sysmgr.domain.SysRole;
import com.hyacinth.core.sysmgr.domain.SysUser;
import com.hyacinth.core.sysmgr.service.IMenuService;
import com.hyacinth.core.sysmgr.service.IRoleService;

@Controller
@RequestMapping("/system/*")
public class PageController {
	
	private IMenuService menuService;
	
	private IRoleService roleService;
	
	@RequestMapping("index.do")
	public String index() throws Exception {
		return "index";
	}
	
	@RequestMapping("home.do")
	public ModelAndView home(HttpSession session) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		SysUser user = (SysUser)session.getAttribute(SysConstant.SYS_SESSION_USER);
		Set<SysRole> roles = user.getRoles();
		/*
		StringBuilder existMenuIds = new StringBuilder();
		for(SysRole r : roles) {
			for(SysMenu m : r.getMenus()) {
				String _tmp = ","+m.getId()+",";
				if(!existMenuIds.indexOf(m.get)) {
					existMenus.add(m.getId());
				}
			}
		}
		*/
		List<SysMenu> usableMenus = new ArrayList<SysMenu>();
		List<Long> existMenus = new ArrayList<Long>();
		List<SysMenu> allMenus = menuService.all();
		for(SysRole r : roles) {
			for(SysMenu m : r.getMenus()) {
				if(!usableMenus.contains(m)) {
					usableMenus.add(m);
				}
			}
		}
		map.put("usableMenus", usableMenus);
		return new ModelAndView("home",map);
	}

	public IMenuService getMenuService() {
		return menuService;
	}

	@Resource
	public void setMenuService(IMenuService menuService) {
		this.menuService = menuService;
	}

	public IRoleService getRoleService() {
		return roleService;
	}

	@Resource
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

}
